/*
 * BookAdd.java
 *
 * Created on __DATE__, __TIME__
 */

package Consult.view;

import java.awt.Image;
import java.awt.Toolkit;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;



/**
 *
 * @author  __USER__
 */
public class BookAdd extends javax.swing.JFrame {

	/**
	 * ���к�
	 */
	private static final long serialVersionUID = 1L;

	BookAddDao bad = new BookAddDao();
	BookQueryDao bqd = new BookQueryDao();

	/** Creates new form BookAdd */
	public BookAdd() {
		initComponents();
		this.setBounds(150, 50, 1050, 650);
		//���ñ���
		setBackgroudImage();
		//�����С���ɱ�
		this.setResizable(false);
		//���ô���ͼ��
		Toolkit tool = this.getToolkit(); //�õ�һ��Toolkit����
		Image myimage = tool.getImage("img/3.png"); //��tool��ȡͼ��
		this.setIconImage(myimage);
	}

	/** 
	 * ��ʼ����¼���屳�� 
	 */
	private void setBackgroudImage() {
		((JPanel) this.getContentPane()).setOpaque(false);
		ImageIcon img = new ImageIcon("img/bookadd.jpg"); // ���ͼƬ  
		JLabel lb_backgroud = new JLabel(img);
		this.getLayeredPane().add(lb_backgroud, new Integer(Integer.MIN_VALUE));
		lb_backgroud.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		bauthor = new javax.swing.JTextField();
		bname = new javax.swing.JTextField();
		bno = new javax.swing.JTextField();
		submit = new javax.swing.JLabel();
		returnMainView = new javax.swing.JLabel();
		back = new javax.swing.JLabel();
		btype = new javax.swing.JComboBox();
		upload = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("\u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf>\u56fe\u4e66\u7ba1\u7406>\u56fe\u4e66\u6dfb\u52a0");

		bauthor.setFont(new java.awt.Font("΢���ź�", 0, 18));
		bauthor.setBorder(null);

		bname.setFont(new java.awt.Font("΢���ź�", 0, 18));
		bname.setBorder(null);

		bno.setFont(new java.awt.Font("΢���ź�", 0, 18));
		bno.setBorder(null);

		submit.setFont(new java.awt.Font("����", 0, 30));
		submit.setForeground(new java.awt.Color(255, 255, 255));
		submit.setText("\u63d0\u4ea4");
		submit.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				submitMouseClicked(evt);
			}
		});

		returnMainView.setFont(new java.awt.Font("����", 0, 20));
		returnMainView.setForeground(new java.awt.Color(255, 255, 255));
		returnMainView.setText("\u8fd4\u56de\u4e3b\u83dc\u5355");
		returnMainView.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				returnMainViewMouseClicked(evt);
			}
		});

		back.setFont(new java.awt.Font("����", 0, 20));
		back.setForeground(new java.awt.Color(255, 255, 255));
		back.setText("\u8fd4\u56de\u4e0a\u4e00\u7ea7");
		back.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				backMouseClicked(evt);
			}
		});

		btype.setFont(new java.awt.Font("΢���ź�", 0, 16));
		btype.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"��ѡ��", "�ƻ�", "ɢ��", "��ѧ", "��װ", "��Ȼ", "��ѧ", "����", "����" }));

		upload.setFont(new java.awt.Font("����", 0, 20));
		upload.setForeground(new java.awt.Color(255, 255, 255));
		upload.setIcon(new javax.swing.ImageIcon(
				"img\\upload.png")); // NOI18N
		upload.setText("\u56fe\u4e66\u6279\u91cf\u589e\u52a0");
		upload.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				uploadMouseClicked(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout.createSequentialGroup().addGap(495, 495,
										495).addComponent(submit)
										.addContainerGap(495, Short.MAX_VALUE))
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								layout
										.createSequentialGroup()
										.addGap(268, 268, 268)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				btype,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				187,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				57,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(
																layout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																		.addComponent(
																				bname,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				244,
																				Short.MAX_VALUE)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								false)
																						.addComponent(
																								bauthor,
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								bno,
																								javax.swing.GroupLayout.Alignment.LEADING,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								244,
																								javax.swing.GroupLayout.PREFERRED_SIZE))))
										.addGap(228, 228, 228)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(upload)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				returnMainView)
																		.addGap(
																				33,
																				33,
																				33)
																		.addComponent(
																				back)))
										.addGap(77, 77, 77)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				43,
																				43,
																				43)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								back)
																						.addComponent(
																								returnMainView))
																		.addGap(
																				89,
																				89,
																				89)
																		.addComponent(
																				upload)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				253,
																				Short.MAX_VALUE))
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				bno,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				31,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				27,
																				27,
																				27)
																		.addComponent(
																				bname,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				31,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				34,
																				34,
																				34)
																		.addComponent(
																				bauthor,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				31,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				39,
																				39,
																				39)
																		.addComponent(
																				btype,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				38,
																				38,
																				38)))
										.addComponent(submit).addGap(137, 137,
												137)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void uploadMouseClicked(java.awt.event.MouseEvent evt) {
		new FileUpload();
	}

	/** 
	 * ����ύ����ͼ��
	 */
	private void submitMouseClicked(java.awt.event.MouseEvent evt) {
		String bno = this.bno.getText();
		String bname = this.bname.getText();
		String bauthor = this.bauthor.getText();
		String btype = this.btype.getSelectedItem().toString();
		BookModel book = new BookModel();
		book.setBno(bno);
		book.setName(bname);
		book.setAuthor(bauthor);
		book.setTname(btype);
		book.setLent("δ����");
		//�ж�ͼ�����Ƿ��ظ�
		List<Object[]> list= bqd.queryB(bno);
		if(list.isEmpty()){
			
			//ͼ������
			bad.insertBook(book);
			JOptionPane.showMessageDialog(null, "�����ɹ�");
		}else{
			JOptionPane.showMessageDialog(null, "ͼ����Ѵ���");
		}

		//���ý���
		reset();
	}

	public void reset() {
		this.bno.setText("");
		this.bname.setText("");
		this.bauthor.setText("");
	}

	/** 
	 * ���������һ��
	 */
	private void backMouseClicked(java.awt.event.MouseEvent evt) {
		//��ת��queryServiceҳ��
		new BookManager().setVisible(true);
		//��ת��ر�ԭ��ҳ��
		this.dispose();
	}

	/** 
	 * �������������
	 */
	private void returnMainViewMouseClicked(java.awt.event.MouseEvent evt) {
		//��ת��queryServiceҳ��
		new MainFrame().setVisible(true);
		//��ת��ر�ԭ��ҳ��
		this.dispose();
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new BookAdd().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JLabel back;
	private javax.swing.JTextField bauthor;
	private javax.swing.JTextField bname;
	private javax.swing.JTextField bno;
	private javax.swing.JComboBox btype;
	private javax.swing.JLabel returnMainView;
	private javax.swing.JLabel submit;
	private javax.swing.JLabel upload;
	// End of variables declaration//GEN-END:variables

}